package com.aiti.lulian.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.dto.CardQuotationDto;
import com.aiti.lulian.entity.CardQuotation;
import com.aiti.lulian.entity.CardQuotationProductPrice;
import com.aiti.lulian.mapper.CardQuotationMapper;
import com.aiti.lulian.mapper.CardQuotationProductPriceMapper;
import com.aiti.lulian.service.CardQuotationProductPriceService;
import com.aiti.lulian.service.CardQuotationService;
import com.aiti.lulian.vo.CardQuotationVo;
import com.aiti.lulian.vo.QuotationProductPriceVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CardQuotationServiceImpl extends BaseSerivceImpl<CardQuotationMapper, CardQuotation> implements CardQuotationService {

    @Autowired
    private CardQuotationMapper cardQuotationMapper;

    @Autowired
    private CardQuotationProductPriceService cardQuotationProductPriceService;

    @Autowired
    private CardQuotationProductPriceMapper cardQuotationProductPriceMapper;

    @Override
    public List<CardQuotationVo> findQuotationListPage(Page<CardQuotation> page, CardQuotationDto cardQuotationDto) {
           List<CardQuotationVo> cardQuotationVo =  cardQuotationMapper.findQuotationListPage(page, cardQuotationDto);
           if (cardQuotationVo!= null)
               for (CardQuotationVo quotationVo : cardQuotationVo) {
                 List<QuotationProductPriceVo>  quotationProductVo = cardQuotationProductPriceService.getPriceByQuotationId(quotationVo.getBaseId());
                   // 计算 quotationCount 的总和
                   int totalQuotationCount = quotationProductVo.stream()
                           .mapToInt(QuotationProductPriceVo::getQuotationCount)
                           .sum();
                   // 计算 quotationPrice 的总和
                   BigDecimal quotationTotalPrice = quotationProductVo.stream()
                           .map(QuotationProductPriceVo::getQuotationTotalPrice)
                           .reduce(BigDecimal.ZERO, BigDecimal::add);
                   quotationVo.setTotalPriceList(quotationTotalPrice);
                   quotationVo.setTotalCountList(totalQuotationCount);
                 quotationVo.setQuotationProductVos(quotationProductVo);
               }
            return cardQuotationVo;
        }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addQuotation(CardQuotationDto cardQuotationDto) {
        CardQuotation cardQuotation = new CardQuotation();
        cardQuotation.setQuotationName(cardQuotationDto.getQuotationName());//报价单名称
        cardQuotation.setCustomId(cardQuotationDto.getCustomId());//客户id
        BigDecimal discount = cardQuotationDto.getDiscount();//报价单折扣
        BigDecimal zk = null;
        if(discount != null){
            //除以100  折扣为百分比
            zk = discount.divide(new BigDecimal(100));
            cardQuotation.setDiscount(discount);//折扣
        }
        cardQuotation.setIsDel("0");
        int insert = cardQuotationMapper.insert(cardQuotation);
        if (insert <= 0){
            return 0;
        }

        if (CollectionUtil.isNotEmpty(cardQuotationDto.getQuotationProductVos())) {
            for (QuotationProductPriceVo quotationProductVo : cardQuotationDto.getQuotationProductVos()) {
                //报价单的产品报价表
                CardQuotationProductPrice cardQuotationProductPrice = new CardQuotationProductPrice();
                cardQuotationProductPrice.setQuotationId(cardQuotation.getBaseId());//报价单id
                cardQuotationProductPrice.setProductId(quotationProductVo.getProductId());//产品id
                Integer quotationCount = quotationProductVo.getQuotationCount();//产品报价数量
                cardQuotationProductPrice.setQuotationCount(quotationCount);
                BigDecimal quotationPrice = quotationProductVo.getQuotationPrice();//产品报价单价
                cardQuotationProductPrice.setQuotationPrice(quotationPrice);
                cardQuotationProductPrice.setProductName(quotationProductVo.getProductName());//产品名称
                cardQuotationProductPrice.setProductSpecification(quotationProductVo.getQuotationProductSpecification());//产品规格
                //计算产品总价 总价 = 数量 * 单价 * 折扣
                //如果折扣为null 则不用乘以折扣
                if(discount != null){
                    BigDecimal totalPrice = new BigDecimal(quotationCount).multiply(quotationPrice).multiply(zk);
                    cardQuotationProductPrice.setQuotationTotalPrice(totalPrice);//产品总价
                }else {
                    BigDecimal totalPrice = new BigDecimal(quotationCount).multiply(quotationPrice);
                    cardQuotationProductPrice.setQuotationTotalPrice(totalPrice);
                }
                int i = cardQuotationProductPriceMapper.insert(cardQuotationProductPrice);
                if (i <= 0){
                    return 0;
                }
            }
        }
        return 6;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQuotation(CardQuotationDto cardQuotationDto) {
        if (cardQuotationDto.getBaseId() != null){
            CardQuotation cardQuotation = cardQuotationMapper.selectById(cardQuotationDto.getBaseId());
            cardQuotation.setQuotationName(cardQuotationDto.getQuotationName());//报价单名称
            cardQuotation.setCustomId(cardQuotationDto.getCustomId());//客户id
            BigDecimal discount = cardQuotationDto.getDiscount();
            BigDecimal zk = null;
            if(discount != null){
                //除以100  折扣为百分比
                 zk = discount.divide(new BigDecimal(100));
                cardQuotation.setDiscount(discount);
            }
            int update = cardQuotationMapper.updateById(cardQuotation);
            if (update <= 0){
                return 0;
            }
            if (CollectionUtil.isNotEmpty(cardQuotationDto.getQuotationProductVos())) {
                //首先删除已有的
                cardQuotationProductPriceMapper.deleteByQuotationId(cardQuotationDto.getBaseId());

                for (QuotationProductPriceVo quotationProductVo : cardQuotationDto.getQuotationProductVos()) {
                    //报价单的产品报价表
                    CardQuotationProductPrice cardQuotationProductPrice = new CardQuotationProductPrice();
                    cardQuotationProductPrice.setQuotationId(cardQuotation.getBaseId());//报价单id
                    cardQuotationProductPrice.setProductId(quotationProductVo.getProductId());//产品id
                    Integer quotationCount = quotationProductVo.getQuotationCount();//产品报价数量
                    cardQuotationProductPrice.setQuotationCount(quotationCount);
                    BigDecimal quotationPrice = quotationProductVo.getQuotationPrice();//产品报价单价
                    cardQuotationProductPrice.setQuotationPrice(quotationPrice);
                    cardQuotationProductPrice.setProductName(quotationProductVo.getProductName());//产品名称
                    cardQuotationProductPrice.setProductSpecification(quotationProductVo.getQuotationProductSpecification());//产品规格
                    //计算产品总价 总价 = 数量 * 单价 * 折扣
                    //如果折扣为null 则不用乘以折扣
                    if(discount!= null){
                        BigDecimal totalPrice = new BigDecimal(quotationCount).multiply(quotationPrice).multiply(zk);
                        cardQuotationProductPrice.setQuotationTotalPrice(totalPrice);//产品总价
                    }else {
                        BigDecimal totalPrice = new BigDecimal(quotationCount).multiply(quotationPrice);
                        cardQuotationProductPrice.setQuotationTotalPrice(totalPrice);
                    }
                    int i = cardQuotationProductPriceMapper.insert(cardQuotationProductPrice);
                    if (i <= 0){
                        return 0;
                    }
                }
            }

        }
        return 6;
    }

    @Override
    public CardQuotationVo getQuotationById(String baseId) {
      CardQuotationVo cardQuotationVo =  cardQuotationMapper.getQuotationById(baseId);
        cardQuotationVo.setQuotationProductVos(cardQuotationProductPriceService.getPriceByQuotationId(baseId));
        return cardQuotationVo;
    }

    @Override
    public int updateQuotationById(String baseId) {
        CardQuotation cardQuotation = cardQuotationMapper.selectById(baseId);
        cardQuotation.setIsDel("1");
        int update = cardQuotationMapper.updateById(cardQuotation);
        if (update <= 0){
            return 0;
        }
        return 6;
    }
}
